const router = require("koa-router")();
const MembersModel = require("../../models/Members")
// 查询数据库
const upload = require("../../config/multer")

router.post('/m1/doAdd',upload.single('file'), async (ctx, next) => {
    console.log(ctx.req.body)
    // console.log(ctx.req.file.filename)
    var {name,age,like,friend} = ctx.req.body;
    /* var avatar = ctx.req.file.filename;
    var host = ctx.request.header.host;
    avatar = `http://${host}/${avatar}`
    var data = new MembersModel({
        name,
        age,
        like:Boolean(like),
        friend:Boolean(friend),
        avatar
    });
    data.save(err=>{
        if(err){throw err};
    })
    ctx.redirect("/m1") */

    
    if(ctx.req.file == undefined){
        var data = new MembersModel({
            name,
            age,
            like:Boolean(like),
            friend:Boolean(friend),
        });
        data.save(err=>{
            if(err){throw err};
        })
    }else{
        var avatar = ctx.req.file.filename;
        var host = ctx.request.header.host;
        avatar = `http://${host}/${avatar}`;
        var data = new MembersModel({
            name,
            age,
            like:Boolean(like),
            friend:Boolean(friend),
            avatar
        });
        data.save(err=>{
            if(err){throw err};
        })
    }
    ctx.redirect("/m1")
}) 
module.exports = router;